/* -----------------------------------------------------------------------
 * random test adventurer
 * -----------------------------------------------------------------------
 */

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"
#include<time.h>
#include <stdlib.h>
// set NOISY_TEST to 0 to remove printfs from output
#define NOISY_TEST 1

int main() {
	// based off of testUpdateCoins  example provided for class
    int i;
    int seed = 1000;
    int numPlayer = 2;

	int r;
    int k[10] = {adventurer, council_room, feast, gardens, mine
               , remodel, smithy, village, baron, great_hall};
    struct gameState G;

	int choice1 = 0;
	int choice2 = 0;
	int choice3 = 0;
	int handPos = 0;
	int bonus;

	int player = 0;

	int j =0;
	
	int random;
	int deck_or_discard = 2;
	int position;
	int treasure[] = {copper, silver, gold, estate, estate, estate, estate, estate};
	int treasureSize = sizeof(treasure)/sizeof(treasure[0]);
	int test;
	int size = 10;
	int full;
	int treasureCount = 0;
	int pass = 1;
	int loops = 100;
	
	srand(time(NULL));
	
	memset(&G, 23, sizeof(struct gameState));   // clear the game state
	r = initializeGame(numPlayer, k, seed, &G); // initialize a new game
	
	//==============================================================================================================
	//repeat tests
	for(j=0; j<loops; j++){
		
		//set variables for initial test state
		pass = 1; //set passing variable
		G.deckCount[player] = 0;
		G.handCount[player] = 0;
		G.discardCount[player] = 0;
		
		treasureCount = 0;
		random = rand()% deck_or_discard;
		full = rand() % size;
		
		//randomly fill deck or discard piles with cards, may be treasure cards or estate cards
		if(random == 0){
			for(i=0; i<full; i++){
				position = G.deckCount[player];
				G.deck[player][position] = treasure[rand()%treasureSize];
				if(G.deck[player][position] == gold || G.deck[player][position] == silver || G.deck[player][position] == copper){
					treasureCount++; // keep count of treasure cards selected
				}
				G.deckCount[player] += 1;
			}
		}
		else{
			for(i=0; i<full; i++){
				position = G.discardCount[player];
				G.discard[player][position] = treasure[rand()%treasureSize];
				if(G.discard[player][position] == gold || G.discard[player][position] == silver || G.discard[player][position] == copper){
					treasureCount++;
				}
				G.discardCount[player] += 1;
			}
		}
	
		//==============================================================================================================
		//play card
		test = cardEffect(adventurer, choice1, choice2, choice3, &G, handPos, &bonus);
		if(test == -1){
			printf("RandomTestCard FAILED, card effect return -1!\n");
		}
		
			
		//player hand count should have added two treasure cards added to hand if availibe
		if(treasureCount >= 2 && G.handCount[player] < 2){
			pass = 0; }
		
		//check cards in hand are treasure cards
		for(i=0; i<2 && i<treasureCount; i++){
			if(G.hand[player][i] == copper || G.hand[player][i] == silver || G.hand[player][i] == gold){
				pass = pass;}
			else{
				pass = 0;}
		}
		//check total cards in play is still the same as number added to deck
		if(G.handCount[player]+G.deckCount[player]+G.discardCount[player] != full){
			pass = 0;}
			
		//check check hand card amount is not negative
		if(G.handCount[player] < 0){
			pass = 0;
		}
		
		//print information about test failure state if needed
		if(pass == 0){
			printf("**FAILED** RandomTestAdventurer: -- Loop = %i, Treasure.availibe = %i, handCount = %i, totalCards = %i, Card.made.availbe = %i\n",	j, treasureCount, G.handCount[player], G.handCount[player]+G.deckCount[player]+G.discardCount[player], full);
		}
	}//end test loop
	
	if(pass == 1){
		printf("RandomTestAdventurer: All tests Passed\n");
	}
	
	
    return 0;
}
